Learn how to create a custom Magento 2 extension to modify behavior of Algolia extension for Magento 2.
CustomAlgolia
boilerplate can be installed with Composer by running the following commands in the command line:
app/code
directory in your Magento 2 base directory where you can make further modifications to the implementation and commit to your own project repository.
app/code/Algolia/CustomAlgolia
:
facet.phtml
template.
This template is used for the InstantSearch feature, to display facets for hits matching the query.
view/frontend/templates/instant/facet.phtml
) to the exact same path in the CustomAlgolia
extension.
The file can now be modified as needed.
algolia_search_handle.xml
and add the following code block to it:
autocomplete.js
file, which implements the autocomplete menu.
view/frontend/web/js/autocomplete.js
) to the exact same path in the CustomAlgolia
extension.
The file can now be modified as needed.
requirejs-config.js
and add the following code block to the config
object.
view/frontend/web/js/hooks.js
.
Specify any dependencies of your hooks. For example:
requirejs-config.js
and add the following code block to the config
object.
algoliaHooks
to ensure that your hooks are registered prior to loading the InstantSearch and Autocomplete libraries that will ultimately invoke them.deps
configuration.
For example:
data-mage-init
attribute<script type="text/x-magento-init">
tagetc/events.xml
file.
For this example, we will create a listener on the algolia_products_index_before_set_settings
event to modify Algolia’s index settings for your products’ index.
etc/events.xml
file:
Observer/UpdateProductsSettings.php
file, and add a new Observer class to it.
execute
block can be modified as needed. In this example, the snippetEllipsisText
setting is modified.